package com.wintrue.ffxs.ui.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.wintrue.ffxs.R;
import com.wintrue.ffxs.base.BaseFragment;
import com.wintrue.ffxs.base.BaseFragmentActivity;
import com.wintrue.ffxs.ui.mine.adapter.MyOrderListAdapter;
import com.wintrue.ffxs.widget.pulltorefresh.PullToRefreshBase;
import com.wintrue.ffxs.widget.pulltorefresh.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lhe on 2017/8/23.
 */

public class MyOrderListFrament extends BaseFragment {

    public static final int ORDER_ALL = 101;
    public static final int ORDER_DAIWANCHENG = 102;
    public static final int ORDER_YIWANCHENG = 103;
    public static final int ORDER_CLOSED = 104;
    private BaseFragmentActivity activity;
    private int status;
    @Bind(R.id.order_list_view)
    PullToRefreshListView listView;
    private MyOrderListAdapter adapter;
    private View loadFaildView;
    private int page = 0;

    public MyOrderListFrament(BaseFragmentActivity activity, int status){
        this.activity = activity;
        this.status = status;
        adapter = new MyOrderListAdapter(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_list,null);
        ButterKnife.bind(this,view);
        listView.setAdapter(adapter);
        loadFaildView = LayoutInflater.from(activity).inflate(R.layout.widget_loading_failed,null);
        listView.setEmptyView(loadFaildView);
        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                getData(false);
                showToastMsg("刷新");
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getData(false);
                showToastMsg("加载更多");
            }
        });
        return view;
    }

    public void notifyData(){
        if(adapter.getCount()>0){
            return;
        }
        page = 1;
        getData(true);
    }

    private void getData(boolean showDialog){
        if(showDialog){
            activity.showWaitDialog("");
        }
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        activity.hideWaitDialog();
                        List<String> list = new ArrayList<String>();
                        list.add("");
                        list.add("");
                        list.add("");
                        list.add("");
                        list.add("");
                        list.add("");
                        list.add("");
                        list.add("");
                        list.add("");
                        if(page == 0){
                            adapter.setList(list);
                        } else{
                            adapter.addList(list);
                        }
                        page++;
                        listView.onRefreshComplete();
                    }
                });
            }
        }.start();
    }

    @Override
    public void onDestroyView() {
        ButterKnife.unbind(this);
        super.onDestroyView();
    }

}
